package com.dkm.orproject.dkmoa.action.flow;

import com.alibaba.fastjson.JSON;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.domain.OaVacateFlow;
import com.dkm.orproject.dkmoa.service.flow.OaVacateFlowService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.Tools;
import io.swagger.annotations.*;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *  @ClassName : OaVacateFlowAction  //类名
 *  @Description : 请假流程控制类  //描述
 *  @Author : 石智诚  //作者
 *  @Date: 2020-11-03 10:18  //时间
 */
@Api

@RestController
@RequestMapping("/OaVacte")
public class OaVacateFlowAction {
    @Resource
    OaVacateFlowService service;

    /**
     * 前台传值工具类
     */
    @Resource
    Tools tools;

    /**
     * 回调值
     */
    int row=0;

    /**
     * 日志
     */
    OaLog log;

    /**
     * 登陆人
     */
    OaPersonnel personnel;

    /**
     * 回调map
     */
    Map<String,Object> map;
   /* public Map<String,Object>*/

    @UserLoginToken
    @GetMapping("findAll")
    public Map<String,Object> findAll(OaVacateFlow flow,
                                      String startTime,
                                      String endTime,
                                      Integer page,
                                      Integer limit,
                                      HttpServletRequest request){
        try{
            List<OaVacateFlow> flowList=service.findAll(flow,page,limit,startTime,endTime);
            Long rowCount=service.rowCount(flow,startTime,endTime);
            return Tools.maps(flowList,rowCount);
        }catch (Exception e){
            addErrorLog(request,"转正流程分页查询",e);
        }
        return null;

    }
    /**
     * 根据id 查询请假流程
     * @param request 服务器请求 用于得到登录人信息
     * @return 离职流程模型
     */
    @UserLoginToken
    @GetMapping("findById/{id}")
    public OaVacateFlow findById(@PathVariable("id") Long id, HttpServletRequest request){
        rowMap(request);
        try{
            return service.selectById(id);
        }catch (Exception e){
            addErrorLog(request,"请假流程id查询",e);
        }
        return null;
    }
    /**
     * 起草节点
     * @param flow 请假流程模型
     * @param request 服务器请求 用于得到登陆人信息
     * @return 回调函数
     */
    @UserLoginToken
    @PostMapping("update")
    public int update(OaVacateFlow flow, HttpServletRequest request){
        rowMap(request);
        try{
            return service.insertSelective(flow,personnel);
        }catch (Exception e){
            addErrorLog(request,"请假更新",e);
            return 500;
        }
    }
    /**
     * 起草节点
     * @param flow 请假流程模型
     * @param request 服务器请求 用于得到登陆人信息
     * @return 回调函数
     */
    @UserLoginToken
    @PostMapping("addOaVacateFlow")
    public int addOaVacateFlow(OaVacateFlow flow, HttpServletRequest request){
        rowMap(request);
        try{
            return service.insertSelective(flow,personnel);
        }catch (Exception e){
            addErrorLog(request,"人员请假起草节点",e);
            return 500;
        }
    }
    /**
     * 部门审核节点通过
     * @param pkVacateId 请假流程模型
     * @param departmentOpinion 意见
     * @param request 服务器请求 用于得到登陆人信息
     * @return 回调函数
     */
    @ApiOperation(value = "部门领导通过", notes = "部门领导通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkVacateId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "departmentOpinion",value = "部门意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })

    @UserLoginToken
    @PostMapping("departmentPass")
    public int departmentPass(Long pkVacateId,String departmentOpinion, HttpServletRequest request){
        rowMap(request);
        try{
            return service.departmentPass(pkVacateId,personnel,departmentOpinion);
        }catch (Exception e){
            addErrorLog(request,"人员部门审核节点",e);
            return 500;
        }
    }
    /**
     * 部门审核节点不通过
     * @param pkVacateId 加班流程id
     * @param fromReason 不同意意见
     * @param request 服务器请求 用于得到登陆人信息
     * @return 回调函数
     */
    @ApiOperation(value = "部门领导不通过", notes = "部门领导不通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkVacateId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "formReason",value = "不同意意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("departmentNoPass")
    public int departmentNoPass(Long pkVacateId,String fromReason, HttpServletRequest request){
        rowMap(request);
        try{
            return service.departmentNoPass(pkVacateId,personnel,fromReason);
        }catch (Exception e){
            addErrorLog(request,"人员部门审核不通过节点",e);
            return 500;
        }
    }
    /**
     * 人事审核节点通过
     * @param pkVacateId 请假流程模型
     * @param  personnelOpinion 人事意见
     * @param request 服务器请求 用于得到登陆人信息
     * @return 回调函数
     */
    @ApiOperation(value = "人事审核节点通过过", notes = "人事审核节点通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkVacateId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "personnelOpinion",value = "人事意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("adminPass")
    public int adminPass(Long pkVacateId,String personnelOpinion ,HttpServletRequest request){
        rowMap(request);
        try{
            return service.adminPass(pkVacateId,personnel,personnelOpinion);
        }catch (Exception e){
            addErrorLog(request,"请假行政部审核不通过审核节点",e);
            return 500;
        }
    }

    /**
     * 人事审核节点不通过
     * @param pkVacateId 请假流程id
     * @param request 服务器请求 用于得到登陆人信息
     * @return 回调函数
     */
    @ApiOperation(value = "人事审核不通过", notes = "人事审核不通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkVacateId",value = "请假流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "formReason",value = "不同意意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("adminNoPass")
    public int adminNoPass(Long pkVacateId,String fromreason, HttpServletRequest request){
        rowMap(request);
        try{
            return service.adminNoPass(pkVacateId,personnel,fromreason);
        }catch (Exception e){
            addErrorLog(request,"请假行政部审核不通过节点",e);
            return 500;
        }
    }
    /**
     * 总经理审核节点
     * @param pkVacateId 请假流程模型
     * @param managerOpinion 总经理意见
     * @param request 服务器请求 用于得到登陆人信息
     * @return 回调函数
     */
    @ApiOperation(value = "总经理审核节点通过过", notes = "总经理审核节点通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkVacateId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "managerOpinion",value = "总经理意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })

    @UserLoginToken
    @PostMapping("managerPass")
    public int managerPass(Long pkVacateId,String managerOpinion, HttpServletRequest request){
        rowMap(request);
        try{
            return service.managerPass(pkVacateId,personnel,managerOpinion);
        }catch (Exception e){
            addErrorLog(request,"请假行政部审核不通过审核节点",e);
            return 500;
        }
    }
    /**
     * 总经理审核节点
     * @param pkVacateId 请假流程id
     * @param request 服务器请求 用于得到登陆人信息
     * @return 回调函数
     */
    @UserLoginToken
    @PostMapping("managerNoPass")
    public int managerNoPass(Long pkVacateId,String fromreason, HttpServletRequest request){
        rowMap(request);
        try{
            return service.adminNoPass(pkVacateId,personnel,fromreason);
        }catch (Exception e){
            addErrorLog(request,"请假行政部审核不通过节点",e);
            return 500;
        }
    }
    /**
     * id删除
     * @param id 请假流程id
     * @param request 服务器请求
     * @return 回调函数
     */
    @UserLoginToken
    @DeleteMapping("deleteById/{id}")
    public Map<String,Object> deleteById(@PathVariable("id") Long id, HttpServletRequest request){
            rowMap(request);
            if(ObjectUtils.isEmpty(id)){
                return tools.rowMap(map,0,null);
            }
            try{
                OaVacateFlow oaVacateFlow = service.selectById(id);
                row = service.deleteById(id);
                log.setLogTitle("人员请假删除");
                log.setLogBody(JSON.toJSON(oaVacateFlow).toString());
                log.setLogType("1");
                tools.rowMap(map,row,log);
            }catch (Exception e){
                addErrorLog(request,"人员请假删除",e);
            }

            return map;
        }
    /**
     * id删除
     * @param ids 请假流程id
     * @param request 服务器请求
     * @return 回调函数
     */
    @UserLoginToken
    @DeleteMapping("deleteAll")
    public Map<String,Object> deleteAll(@RequestParam(value = "ids[]",required = false)Long[] ids, HttpServletRequest request){
        rowMap(request);
        if(ObjectUtils.isEmpty(ids)){
            return tools.rowMap(map,0,null);
        }
        try{
            //将long 类型转换为list 类型
            List<Long> list = Arrays.asList(ids);
            //为删除做备份
            List<OaVacateFlow> flowList = service.selectAll(list);
            row=service.deleteAll(list);
            log.setLogTitle("人员请假删除");
            log.setLogBody(JSON.toJSON(flowList).toString());
            log.setLogType("1");
            tools.rowMap(map,row,log);
        }catch (Exception e){
            addErrorLog(request,"人员请假删除",e);
        }
        return map;
    }
    /**
     * 查询报错日志新增
     * @param request 请求 用于得到当前登录人和ip地址
     * @param title 模块标题
     * @param e 异常参数
     */
    private void addErrorLog(HttpServletRequest request,String title,Exception e){
        e.printStackTrace();
        rowMap(request);
        log.setLogType("0");
        log.setLogBody(e.getLocalizedMessage());
        log.setLogTitle(title);
        if(ObjectUtils.isEmpty(personnel)){
            log.setCreatedBy(personnel.getPersonnelName());
        }
        tools.errorMap(log);
    }

    /**
     * 简化代码
     * @param request 服务器请求
     */
    private void rowMap(HttpServletRequest request){
        //新建返回Map模型
        map=new HashMap<>(16);
        //获取登陆人信息
        personnel= (OaPersonnel) request.getSession().getAttribute("user");
        //为空则清空map
        if(ObjectUtils.isEmpty(personnel)){
            return;
        }
        //新建日志
        log=new OaLog();
        //得到ip
        log.setLogIp(request.getRemoteAddr());
        //登陆人名称
        log.setCreatedBy(personnel.getPersonnelName());
    }
    /**
     * 日期格式化
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        // true:允许输入空值，false:不能为空值
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
